//
//  JMEnrollmentPolicyViewController.m
//  VoluntaryStar
//
//  Created by xiaorui on 15/9/6.
//  Copyright (c) 2015年 ios. All rights reserved.
//

#import "JMEnrollmentPolicyViewController.h"
#import "JMEnrollmentPolicyViewMode.h"
#import "JMEnrollmentCell.h"
#import "JMPolicyMode.h"

#import "JMEnrollmentInfoViewController.h"

@interface JMEnrollmentPolicyViewController (){
    
    int lastId;
}
@end

@interface JMEnrollmentPolicyViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataArray;


@end

@implementation JMEnrollmentPolicyViewController

- (UITableView *)tableView
{
    if (!_tableView) {
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.backgroundColor = [UIColor clearColor];
        tableView.backgroundView.backgroundColor = [UIColor clearColor];
        tableView.frame = self.view.bounds;
        _tableView = tableView;
    }
    return _tableView;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc]initWithCapacity:0];
    }
    return _dataArray;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.navigationController.navigationBarHidden = NO;
    self.backBtn.hidden =YES;
    self.navigationItem.title =@"招生政策";
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    lastId = 0;
    [self setupRefresh];
}
#pragma mark 上下拉刷新数据
- (void)setupRefresh{
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRereshing)];
    [self.tableView.header beginRefreshing];
    
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRereshing)];
}

-(void)headerRereshing{
    lastId = 0;
    [self getMessageFromeServer];
}
-(void)footerRereshing{
    [self getMessageFromeServer];
}

-(void)getMessageFromeServer{
    
    [JMTools showAlertView:self.view];
    JMConfigure *cf = [JMConfigure sharedConfigureManger];
    if(cf.user){
        NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
        [dict setObject:cf.user.userId forKey:@"user"];
        [dict setObject:cf.user.token forKey:@"token"];
        [dict setObject:[NSString stringWithFormat:@"%d",lastId] forKey:@"last"];
        [dict setObject:@"15"forKey:@"size"];
        [JMEnrollmentPolicyViewMode fetchEnrollmentPolicyListParametes:dict block:^(id data, NSError *error, id currData) {
            [JMTools hiddenAlerView:self.view];

            if([[data objectForKey:@"status"] integerValue] == 1){
                if(lastId == 0)[self.dataArray removeAllObjects];
                NSArray *array = (NSArray *)currData;
                if(lastId == [[currData lastObject] policyId] || [currData count]==0){
                    [JMTools showAlertView:self.view message:@"没有更多数据"];
                }else{
                    [self.dataArray addObjectsFromArray:array];
                    lastId = [[currData lastObject] policyId];
                    [self.tableView reloadData];
                    [JMTools showAlertView:self.view message:data[@"message"]];
                }
            }else{
                [JMTools showAlertView:self.view message:data[@"message"]];
            }
            [self.tableView.header endRefreshing];
            [self.tableView.footer endRefreshing];
        }];
    }
}

#pragma mark  tableView delegate dasouce
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectio{
    return  self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * str=@"ID";
    JMEnrollmentCell * cell = [tableView dequeueReusableCellWithIdentifier:str];
    if(!cell){
        cell = [[JMEnrollmentCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:str];
    }
    JMPolicyMode *mode = [self.dataArray objectAtIndex:indexPath.row];
    [cell upCellInfoWith:mode];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    JMEnrollmentInfoViewController *vc =[JMEnrollmentInfoViewController new];
    vc.mode = self.dataArray[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UI_SCREEN_WITH *0.453125;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
